package hj212

import (
	"github.com/funny/link"
	"go.uber.org/zap"

	"gitee.com/whoiszhangben/hj212_recSvc/core/hj212/protocol"
	"gitee.com/whoiszhangben/hj212_recSvc/global"
)

type Session struct {
	deviceID string
	session  *link.Session
	server   *Server
}

// newSession 创建session
func newSession(server *Server, sess *link.Session) *Session {
	return &Session{
		session: sess,
		server:  server,
	}
}

// ID 获取sessionID
func (session *Session) ID() uint64 {
	return session.session.ID()
}

// Send 发送消息
func (session *Session) Send(message protocol.Message) error {
	return session.session.Send(message)
}

func (session *Session) DataReply(message protocol.Message) error {
	newMessage := message
	newMessage.Header.ST = 91
	newMessage.Header.CN = 9014
	newMessage.Header.Flag = 4
	newMessage.Body = &protocol.HJ212_9014{}
	return session.Send(newMessage)
}

// message 消息事件
func (session *Session) message(message *protocol.Message) {
	if message.Header.MN != "" {
		old := session.deviceID
		if old != "" && message.Header.MN != old {
			global.GVA_LOG.Warn("deviceID is inconsistent", zap.Uint64("sessionID", session.session.ID()), zap.String("old", old), zap.String("new", message.Header.MN))
		}

		session.deviceID = message.Header.MN
	}
}
